package com.dgut.service.serviceImpl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.dgut.mapper.OrderFlagMapper;
import com.dgut.pojo.LegalRecourse;
import com.dgut.pojo.OrderFlag;
import com.dgut.pojo.QualificationCertificate;
import com.dgut.service.NoticeService;
import com.dgut.service.OrderFlagService;
import com.dgut.service.ProcessVoucherService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"all"})
@Service
public class OrderFlagServiceImpl implements OrderFlagService {
    @Resource
    private OrderFlagMapper orderFlagMapper;
    @Resource
    private NoticeService noticeService;
    @Resource
    private ProcessVoucherService processVoucherService;
    /**
     * start-zwb
     */
    //采买方获取自己的订单表
    public Object getOrderList(PageRequestInfo pageRequestInfo,String type,String state)
    {
        Page<OrderFlag> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        QueryWrapper<OrderFlag> queryWrapper = new QueryWrapper<>();
        if(type!=null)
            queryWrapper.like("type",type);
        if(state!=null)
            queryWrapper.like("state",state);
        Integer buyerId = Integer.valueOf((String) StpUtil.getLoginId());
        queryWrapper.eq("buyer_id",buyerId);
        queryWrapper.orderByDesc("create_time");
        Page<OrderFlag> resultPage = orderFlagMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    //根据id查看订单详情
    public OrderFlag getOrder(Integer id)
    {
        return orderFlagMapper.selectById(id);
    }
    //申请取消订单
    public Object requestCancellationOfOrder(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<OrderFlag> queryWrapper = new QueryWrapper<>();
        OrderFlag orderFlag = orderFlagMapper.selectById(id);
        if(orderFlag!=null)
        {
            orderFlag.setExceptionIdentification(ExceptionIdentificationForOrder.CANCELING);
            queryWrapper.eq("id",id);
            if(orderFlagMapper.update(orderFlag,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);

                /**
                 * 申请取消订单通知
                 */
                noticeService.cancelOrderRequestNotice(orderFlag.getId(),orderFlag.getServiceId());
            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }


        return resultMap;
    }
    //订单付款
    public Object payForOrder(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<OrderFlag> queryWrapper = new QueryWrapper<>();
        OrderFlag orderFlag = orderFlagMapper.selectById(id);
        if(orderFlag!=null)
        {
            orderFlag.setState(StateForOrder.WAITING_FOR_PROCESSING);
            orderFlag.setStateTime(new Date());
            orderFlag.setPaymentOrNot(OnOrOff.ON);
            queryWrapper.eq("id",id);
            if(orderFlagMapper.update(orderFlag,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);

                /**
                 * 订单状态改变通知
                 */
                noticeService.orderChangedStateNotice(orderFlag);

            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }
        return resultMap;
    }
    //订单确认
    public Object confirmOrder(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<OrderFlag> queryWrapper = new QueryWrapper<>();
        OrderFlag orderFlag = orderFlagMapper.selectById(id);
        if(orderFlag!=null)
        {
            orderFlag.setState(StateForOrder.FINISHED);
            orderFlag.setStateTime(new Date());
            orderFlag.setFinishTime(new Date());
            queryWrapper.eq("id",id);
            if(orderFlagMapper.update(orderFlag,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);

                /**
                 * 订单状态改变通知
                 */
                noticeService.orderChangedStateNotice(orderFlag);

            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }
        return resultMap;
    }

    /**
     * end-zwb
     */


    /**
     * start-gxd
     */

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getWaitAssignedOrder(PageRequestInfo pageRequestInfo) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<OrderFlag> wrapper = new QueryWrapper<>();
        wrapper.eq("state",StateForOrder.WAITING_FOR_ASSIGNED);
        Page<OrderFlag> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        Page<OrderFlag> resultPage = orderFlagMapper.selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getWaitReviewOrder(PageRequestInfo pageRequestInfo) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<OrderFlag> wrapper = new QueryWrapper<>();
        wrapper.eq("state",StateForOrder.WAITING_FOR_REVIEW);
        wrapper.orderByDesc("create_time");
        Page<OrderFlag> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        Page<OrderFlag> resultPage = orderFlagMapper
                .selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getCancelingOrder(PageRequestInfo pageRequestInfo) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<OrderFlag> wrapper = new QueryWrapper<>();
        wrapper.eq("exception_identification",ExceptionIdentificationForOrder.CANCELING);
        wrapper.orderByDesc("create_time");
        Page<OrderFlag> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        Page<OrderFlag> resultPage = orderFlagMapper
                .selectPage(page,wrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object getAllOrder(PageRequestInfo pageRequestInfo) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<OrderFlag> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        Page<OrderFlag> resultPage = orderFlagMapper
                .selectPage(page,queryWrapper);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    //订单指派
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object assignOrder(Integer orderId , Integer serviceId){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        String payment = StateForOrder.WAITING_FOR_PAYMENT;
        orderFlagMapper.assignOrder(orderId,serviceId,payment);

        noticeService.assignOrder(orderId,serviceId);//指派订单通知

        resultMap.put("message","指派成功！");
        return resultMap;
    }

    //订单审核通过
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object reviewOrder(Integer orderId,String agreeOrNot){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        String confirmation = StateForOrder.WAITING_FOR_CONFIRMATION;       //待确认
        String confirmation1 = StateForOrder.WAITING_FOR_PROCESSING;        //待处理
        OrderFlag orderFlag = orderFlagMapper.selectById(orderId);
        if (agreeOrNot.equals("通过")){
            orderFlagMapper.reviewOrderSuccessd(orderId,confirmation);
            resultMap.put("message","订单审核通过！");
            noticeService.resultProcessVoucher(orderId,orderFlag.getServiceId(),true);
        }
        else {

            orderFlagMapper.reviewOrderFailed(orderId,confirmation1);
            processVoucherService.deleteByOrderId(orderId);
            resultMap.put("message","订单审核不通过");
            noticeService.resultProcessVoucher(orderId,orderFlag.getServiceId(),false);
        }
        /**
         * 通知
         */
        noticeService.orderChangedStateNotice(orderFlag);
        return resultMap;
    }

    //订单取消
    @Override
    @SaCheckLogin
    @SaCheckRole(SelectRole.ADMIN)
    public Object cancelOrder(Integer orderId){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        String canceled = ExceptionIdentificationForOrder.CANCELED;
        int i = orderFlagMapper.cancelOrder(orderId,canceled);
        OrderFlag orderFlag = orderFlagMapper.selectById(orderId);
        if(i!= 0){
            resultMap.put("message","取消成功！");
            noticeService.cancelOrderRequestNotice(orderId,orderFlag.getServiceId());
            noticeService.cancelOrderResultNotice(orderId,orderFlag.getBuyerId());
        }
        else{
            resultMap.put("messgae","取消失败！");
        }
        return resultMap;
    }
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */

    @Override
    public PageResult nonPaymentOrderListForServiceProvider(PageRequestInfo pageRequestInfo,int serviceProviderId) {
        Page<OrderFlag> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<OrderFlag> pageResult = orderFlagMapper.selectPage(page, Wrappers.<OrderFlag>lambdaQuery().eq(OrderFlag::getState, StateForOrder.WAITING_FOR_PAYMENT).eq(OrderFlag::getServiceId,serviceProviderId).orderByDesc(OrderFlag::getCreateTime));
        return PageUtils.getPageResult(pageResult);
    }

    @Override
    public PageResult untreatedOrderListForServiceProvider(PageRequestInfo pageRequestInfo,int serviceProviderId) {
        Page<OrderFlag> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<OrderFlag> pageResult = orderFlagMapper.selectPage(page, Wrappers.<OrderFlag>lambdaQuery().eq(OrderFlag::getState, StateForOrder.WAITING_FOR_PROCESSING).eq(OrderFlag::getServiceId,serviceProviderId).orderByDesc(OrderFlag::getCreateTime));
        return PageUtils.getPageResult(pageResult);
    }

    @Override
    public PageResult treatedOrderListForServiceProvider(PageRequestInfo pageRequestInfo,int serviceProviderId) {
        Page<OrderFlag> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<OrderFlag> pageResult = orderFlagMapper.selectPage(page, Wrappers.<OrderFlag>lambdaQuery().eq(OrderFlag::getState, StateForOrder.WAITING_FOR_REVIEW).eq(OrderFlag::getServiceId,serviceProviderId).or().eq(OrderFlag::getState, StateForOrder.WAITING_FOR_CONFIRMATION).eq(OrderFlag::getServiceId,serviceProviderId).orderByDesc(OrderFlag::getCreateTime));
        return PageUtils.getPageResult(pageResult);
    }

    @Override
    public PageResult finishedOrderListForServiceProvider(PageRequestInfo pageRequestInfo,int serviceProviderId) {
        Page<OrderFlag> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<OrderFlag> pageResult = orderFlagMapper.selectPage(page, Wrappers.<OrderFlag>lambdaQuery().eq(OrderFlag::getState, StateForOrder.FINISHED).eq(OrderFlag::getServiceId,serviceProviderId).orderByDesc(OrderFlag::getCreateTime));
        return PageUtils.getPageResult(pageResult);
    }

    @Override
    public OrderFlag getOrderForServiceProvide(int orderId) {
        return orderFlagMapper.selectById(orderId);
    }

    @Override
    public boolean changeOrderState(int orderId, String state) {
        if(orderFlagMapper.changeOrderState(orderId, state) >= 1){
            return true;
        }
        /**
         * 订单状态改变通知;
         */
        OrderFlag orderFlag = orderFlagMapper.selectById(orderId);
        noticeService.orderChangedStateNotice(orderFlag);
        return false;
    }
    /**
     * end-hwf
     */
}
